/**
 * 日期时间格式化
 * @param String format 必传，日期格式模板 如 'yyyy-MM-dd hh:mm'
 * @param Date timestamp 时间，不传时默认为当前时间
 * @param Number type 格式化类型，默认为 24
 * */
export function formatDateTime(format, timestamp, type) {
  const newDate = new Date()
  if (timestamp) {
    newDate.setTime(timestamp)
  }
  let hours = newDate.getHours()
  if (type === 12) {
    const ampm = hours >= 12 ? '下午' : '上午'
    hours = hours % 12
    hours = hours === 0 && ampm === '下午' ? 12 : hours
    hours = hours < 10 ? '0' + hours : hours
    hours = hours ? ampm + hours : ampm + 12
  }

  const date = {
    'M+': newDate.getMonth() + 1,
    'd+': newDate.getDate(),
    'h+': hours,
    'm+': newDate.getMinutes(),
    's+': newDate.getSeconds(),
    'q+': Math.floor((newDate.getMonth() + 3) / 3),
    'S+': newDate.getMilliseconds()
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (newDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1
        ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return format
}

/**
 * 获取时间上下午
 * @param {Number} timestamp 时间戳
 */
export function formatAmorPm(timestamp) {
  const newDate = new Date()
  newDate.setTime(timestamp)
  const hours = newDate.getHours()
  const ampm = hours >= 12 ? '下午' : '上午'
  return ampm
}

export function formatWeekTime(format, timestamp) {
  const newDate = new Date()
  newDate.setTime(timestamp)
  const hours = newDate.getHours()

  // 星期几
  const week = newDate.getDay()
  const weekObj = {
    '0': '周日',
    '1': '周一',
    '2': '周二',
    '3': '周三',
    '4': '周四',
    '5': '周五',
    '6': '周六'
  }

  const date = {
    'M+': newDate.getMonth() + 1,
    'd+': newDate.getDate(),
    'h+': hours,
    'm+': newDate.getMinutes(),
    's+': newDate.getSeconds(),
    'q+': Math.floor((newDate.getMonth() + 3) / 3),
    'S+': newDate.getMilliseconds(),
    'W+': weekObj[week]
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (newDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1
        ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return format
}

// 判断是否闰年
export function isLeapYear(year) {
  return (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0)
}

// 获取每月天数
export function getMonthDayNum(year, month) {
  const day31 = [1, 3, 5, 7, 8, 10, 12]
  const day30 = [4, 6, 9, 11]

  if (day31.includes(month)) {
    return 31
  } else if (day30.includes(month)) {
    return 30
  } else {
    if (isLeapYear(year)) {
      return 29
    } else {
      return 28
    }
  }
}

// 获取次月的最后一天
export function nextMonthLastDate(date) {
  if (!date) {
    date = new Date()
  }
  let year = date.getFullYear()
  let month = date.getMonth() + 2
  if (month > 12) {
    month = month - 12
    year = year + 1
  }

  const day = getMonthDayNum(year, month)
  return new Date(`${year}/${month}/${day} 23:59:00`)
}

// 获取下一年
export function lastYearDate(date) {
  if (!date) {
    date = new Date()
  }
  const lastYear = date.getFullYear() - 1
  date.setFullYear(lastYear)
  return date
}

// 获取N年前 ，或N年后
export function getNYearDate(date, n) {
  if (!date) {
    date = new Date()
  }
  const newYear = date.getFullYear() - n
  date.setFullYear(newYear)
  return date
}

// 获取N天前或N天后的日期
export function getNDate(n) {
  return new Date(new Date() - 0 + n * 86400000)
}
